package com.musicapp;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

public class TotalAdapter extends RecyclerView.Adapter<TotalAdapter.ViewHolder>{
    List<SongModel> songList = FirstFragment.songList;
    public Song song = new Song();
    public static List<ItemModel> itemList1;
    public static List<ItemModel> itemList2;
    public static List<ItemModel> itemList3;
    public static List<ItemModel> itemList4;
    public static EditAdapter adapter1;
    public static ItemAdapter adapter2;
    public static TextAdapter1 adapter3;
    public static ItemAdapter2 adapter4;
    public static TextAdapter2 adapter5;
    public static ItemAdapter3 adapter6;
    public static TextAdapter3 adapter7;
    public static ItemAdapter4 adapter8;

    public TotalAdapter(List<ItemModel> itemList1,List<ItemModel> itemList2,List<ItemModel> itemList3,List<ItemModel> itemList4){
        this.itemList1 = itemList1;
        this.itemList2 = itemList2;
        this.itemList3 = itemList3;
        this.itemList4 = itemList4;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recyclerview,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        adapter1 = new EditAdapter();
        adapter2 = new ItemAdapter(itemList1);
        adapter3 = new TextAdapter1();
        adapter4 = new ItemAdapter2(itemList2);
        adapter5 = new TextAdapter2();
        adapter6 = new ItemAdapter3(itemList3);
        adapter7 = new TextAdapter3();
        adapter8 = new ItemAdapter4(itemList4);
        holder.bind();

        adapter2.setmOnItemClickListen(new ItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(view.getId()==R.id.banner){
                    Context context = view.getContext();
                    Intent intent = new Intent(context, song.getClass());
                    ItemModel currentItem = itemList1.get(position);
                    intent.putExtra("author",currentItem.getAuthor());
                    intent.putExtra("image",currentItem.getCoverImage());
                    intent.putExtra("musicname",currentItem.getMusicName());
                    intent.putExtra("musicurl",currentItem.getMusicUrl());
                    intent.putExtra("lyricurl",currentItem.getLyricUrl());
                    context.startActivity(intent);
                }else if(view.getId()==R.id.add){
                    songList.add(itemList1.get(position).transModel());
                }
            }
        });

        adapter4.setmOnItemClickListen(new ItemAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(view.getId()==R.id.banner){
                    Context context = view.getContext();
                    Intent intent = new Intent(context, song.getClass());
                    ItemModel currentItem = itemList2.get(position);
                    intent.putExtra("author",currentItem.getAuthor());
                    intent.putExtra("image",currentItem.getCoverImage());
                    intent.putExtra("musicname",currentItem.getMusicName());
                    intent.putExtra("musicurl",currentItem.getMusicUrl());
                    intent.putExtra("lyricurl",currentItem.getLyricUrl());
                    context.startActivity(intent);
                }else if(view.getId()==R.id.add){
                    songList.add(itemList2.get(position).transModel());
                }
            }
        });

        adapter6.setmOnItemClickListen(new ItemAdapter3.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(view.getId()==R.id.image){
                    Context context = view.getContext();
                    Intent intent = new Intent(context, song.getClass());
                    ItemModel currentItem = itemList3.get(position);
                    intent.putExtra("author",currentItem.getAuthor());
                    intent.putExtra("image",currentItem.getCoverImage());
                    intent.putExtra("musicname",currentItem.getMusicName());
                    intent.putExtra("musicurl",currentItem.getMusicUrl());
                    intent.putExtra("lyricurl",currentItem.getLyricUrl());
                    context.startActivity(intent);
                }else if(view.getId()==R.id.add){
                    songList.add(itemList3.get(position).transModel());
                }

            }
        });

        adapter8.setmOnItemClickListen(new ItemAdapter4.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(view.getId()==R.id.image){
                    Context context = view.getContext();
                    Intent intent = new Intent(context, song.getClass());
                    ItemModel currentItem = itemList4.get(position);
                    intent.putExtra("author",currentItem.getAuthor());
                    intent.putExtra("image",currentItem.getCoverImage());
                    intent.putExtra("musicname",currentItem.getMusicName());
                    intent.putExtra("musicurl",currentItem.getMusicUrl());
                    intent.putExtra("lyricurl",currentItem.getLyricUrl());
                    context.startActivity(intent);
                }else if(view.getId()==R.id.add){
                    songList.add(itemList4.get(position).transModel());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        RecyclerView child1;
        RecyclerView child2;
        RecyclerView child3;
        RecyclerView child4;
        RecyclerView child5;
        RecyclerView child6;
        RecyclerView child7;
        RecyclerView child8;
        public ViewHolder(@NonNull View itemview){
            super(itemview);
            child1 = itemview.findViewById(R.id.recyclerView1);
            child1.setLayoutManager(new LinearLayoutManager(itemview.getContext()));
            child2 = itemview.findViewById(R.id.recyclerView2);
            child2.setLayoutManager(new LinearLayoutManager(itemview.getContext(),LinearLayoutManager.HORIZONTAL,false));
            child3 = itemview.findViewById(R.id.recyclerView3);
            child3.setLayoutManager(new LinearLayoutManager(itemview.getContext()));
            child4 = itemview.findViewById(R.id.recyclerView4);
            child4.setLayoutManager(new LinearLayoutManager(itemview.getContext(),LinearLayoutManager.HORIZONTAL,false));
            child5 = itemview.findViewById(R.id.recyclerView5);
            child5.setLayoutManager(new LinearLayoutManager(itemview.getContext()));
            child6 = itemview.findViewById(R.id.recyclerView6);
            child6.setLayoutManager(new LinearLayoutManager(itemview.getContext()));
            child7 = itemview.findViewById(R.id.recyclerView7);
            child7.setLayoutManager(new LinearLayoutManager(itemview.getContext(),LinearLayoutManager.VERTICAL,false));
            child8 = itemview.findViewById(R.id.recyclerView8);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(itemview.getContext(), 2);
            child8.setLayoutManager(gridLayoutManager);

        }
        void bind(){
            child1.setAdapter(adapter1);
            child2.setAdapter(adapter2);
//            轮播图
            PagerSnapHelper snapHelper = new PagerSnapHelper();
            snapHelper.attachToRecyclerView(child2);
            adapter2.startAutoPlay();

            child3.setAdapter(adapter3);
            child4.setAdapter(adapter4);
            child5.setAdapter(adapter5);
            child6.setAdapter(adapter6);
            child7.setAdapter(adapter7);
            child8.setAdapter(adapter8);
        }
    }
}